package com.google.code.UtopiaXml;

/**
 * Class representing xs:sequence with maxOccurs&lt;=1
 *
 * @author Michael Simacek
 */
public class SingleSequence extends SingleIndicator {

    public SingleSequence() {
    }

    /**
     * Creates empty sequence with given name
     *
     * @param name indicator name
     */
    @Deprecated
    public SingleSequence(String name) {
    }

    @Override
    public void generateParserBlock(CodeBuilder cb, String parentObject, String nodeName) {
        cb.appendLine("//begin SingleSequence");
        for (IndicatorContent item : getContent()) {
            StringBuilder cond = new StringBuilder();
            cond.append("if (").append(nodeName).append(" != null && (");
            for (String match : item.getMatchStrings()) {
                cond.append("\"").append(match).append("\"").append(".equals(")
                        .append(nodeName).append(".getNodeName()").append(") || ");
            }
            if (cond.length() > 3) {
                cond.setLength(cond.length() - 4);
            }
            cond.append(")) {");
            cb.appendLine(cond.toString());
            item.generateParserBlock(cb, parentObject, nodeName);
            cb.appendLine("}");
        }
        cb.appendLine("//end SingleSequence");
    }

    @Override
    protected Indicator shallowCopy() {
        return new SingleSequence();
    }
}
